/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/framework/dao/expression/In.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Id: In.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Author: lanzhouxue $
 * $Date: 2012-07-02 11:09:12 +0800 (周一, 02 七月 2012) $
 * $Revision: 1034 $
 */
package com.elancom.framework.dao.expression;

import java.util.Collection;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

/**
 * @author elancom
 * @date Apr 13, 2012
 */
public class In implements Condition {
	private static final long serialVersionUID = -2108772599282612773L;

	private final String propertyName;

	private final Object value;

	public In(String propertyName, Object value) {
		super();
		this.propertyName = propertyName;
		this.value = value;
	}

	@Override
	public Criterion toCriterion() {
		if (value instanceof Collection) {
			return Restrictions.in(propertyName, (Collection<?>) value);
		} else if (value instanceof Object[]) {
			return Restrictions.in(propertyName, (Object[]) value);
		} else {
			return Restrictions.in(propertyName, new Object[] { value });
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public String toHQLString() {
		final StringBuilder sb = new StringBuilder();
		sb.append(propertyName).append(" in (");

		if (value instanceof Collection) {
			sb.append(TypeUtils.toValue((Collection) value));
		} else if (value instanceof Object[]) {
			sb.append(TypeUtils.toValue((Object[]) value));
		} else {
			sb.append(value);
		}

		sb.append(")");
		return sb.toString();
	}
}
